package zhongbo.java.air.dictionary.service.serverImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhongbo.java.air.common.exception.NoStackLogException;
import zhongbo.java.air.common.util.ObjectIsNullUtil;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.dictionary.mapper.AirportMapper;
import zhongbo.java.air.dictionary.model.AirCraft;
import zhongbo.java.air.dictionary.model.Airport;
import zhongbo.java.air.dictionary.model.Dto.AirportPageDto;
import zhongbo.java.air.dictionary.model.Dto.PageDto;
import zhongbo.java.air.dictionary.service.AirportServer;

import java.util.Arrays;

@Service
public class AirportServerImpl implements AirportServer {

    @Autowired
    private AirportMapper airportMapper;

    //增加机场信息
    @Override
    public void addAirport(Airport airport){

        boolean b = ObjectIsNullUtil.allNonIdFieldsNonNull(airport);


        if(!b){
            throw new NoStackLogException("属性为空,请检查");
        }
        if(airport.getIcaoCode().length()>4){
            throw new NoStackLogException("机场代码超过4个字符");
        }
        if(airport.getIataCode().length()>3){
            throw new NoStackLogException("IATA代码超过3个字符");
        }
        airportMapper.insert(airport);
    }

    //删除机场信息
    @Override
    public void deleteById(String id){
        if(id==null||id.isEmpty()){
            throw new NoStackLogException("id不能为空");
        }
        LambdaQueryWrapper<Airport> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Airport::getId,id);
        airportMapper.delete(lqw);
    }

    //修改机场信息,提供id
    @Override
    public void updateAirport(Airport airport){
        if(airport.getId()==null||airport.getId().isEmpty()){
            throw new NoStackLogException("id不能为空");
        }

        if(airport.getIcaoCode()!=null&&airport.getIcaoCode().length()>4){
            throw new NoStackLogException("机场代码超过4个字符");
        }
        if(airport.getIcaoCode()!=null&&airport.getIataCode().length()>3){
            throw new NoStackLogException("IATA代码超过3个字符");
        }
        LambdaQueryWrapper<Airport> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Airport::getId,airport.getId());
        airportMapper.update(airport,lqw);
    }

    //分页查询，可以排序
    @Override
    public PageVo<Airport> selectPage(PageDto airportPageDto){

        boolean get = Arrays.stream(airportPageDto.getClass().getDeclaredMethods())
                .filter(method -> method.getName().startsWith("get"))
                .map(method -> {
                    try {
                        return method.invoke(airportPageDto);
                    } catch (Exception e) {
                        return null;
                    }
                })
                .noneMatch(value -> value == null);

        if(get){
            throw new NoStackLogException("关键属性不能为空");
        }

        QueryWrapper<Airport> qw = new QueryWrapper<>();
        qw.orderBy(true,airportPageDto.isAsc(),airportPageDto.getSortField());
        Page<Airport> page = new Page<>(airportPageDto.getPageNum(),airportPageDto.getPageSize());
        Page<Airport> airportPage = airportMapper.selectPage(page, qw);
        PageVo<Airport> airportPageVoa = new PageVo<>();
        airportPageVoa.setPage(airportPage.getPages());
        airportPageVoa.setPageSize(airportPage.getSize());
        airportPageVoa.setPageTotal(airportPage.getTotal());
        airportPageVoa.setRecords(airportPage.getRecords());
        return airportPageVoa;
    }
}
